package chapter14

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.rdd.RDD

/**
 * author: yuhui
 * descriptions:
 * WordCount实现的第四种方式：groupByKey+map
 *
 * date: 2024 - 10 - 29 11:35 上午
 */
object WordCount4 {
  def main(args: Array[String]): Unit = {
    val config: SparkConf = new SparkConf().setMaster("local[*]").setAppName("WordCount4")

    val sc: SparkContext = new SparkContext(config)

    val lines: RDD[String] = sc.textFile("doc/output_buffered.txt")

    val groupByKeyRDD: RDD[(String, Iterable[Int])] = lines.flatMap(_.split(",")).map((_, 1)).groupByKey()

    groupByKeyRDD.map(tuple => {
      (tuple._1, tuple._2.sum)
    })
      .collect()
      .foreach(println)

  }
}